package com.syh.jn.scm.service.impl.supplier.base.goods;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.base.goods.Origin;
import com.syh.jn.scm.domain.supplier.repository.base.goods.OriginRepository;
import com.syh.jn.scm.dto.supplier.base.goods.goodsOrigin.OriginMto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsOrigin.OriginSto;
import com.syh.jn.scm.dto.supplier.base.goods.goodsOrigin.OriginVo;
import com.syh.jn.scm.service.api.supplier.base.goods.OriginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产地
 */
@Service
@Transactional
public class OriginServiceImpl extends AbstractServiceImpl<OriginSto, OriginMto, OriginVo, Origin, Long> implements OriginService {
  @Autowired
  private OriginRepository originRepository;

  @Override
  protected AggregateRepository<Origin, Long> getAggregateRepository() {
    return originRepository;
  }

  /**
   * 保存对象
   * @param sto
   * @return
   */
  @Override
  public Long save(OriginSto sto) {
    ExtAssert.notNull(sto, "保存对象为空");
    ValidationUtils.validate(sto);

    Origin origin = Origin.createdOriginBySupply(sto);
    return originRepository.add(origin);
  }

  /**
   * 修改对象
   * @param mto
   */
  @Override
  public void update(OriginMto mto) {
    ExtAssert.notNull(mto, "修改对象为空");
    ValidationUtils.validate(mto);

    //根据id获取产地并且判空
    Origin origin = this.getOriginById(mto.getId());

    origin.modify(mto);
    originRepository.update(origin);
  }

  /**
   * 删除对象
   * @param id
   */
  @Override
  public void delete(Long id) {
    //根据id获取产地并且判空
    Origin origin = this.getOriginById(id);

    origin.destroy();
    originRepository.removeById(id);
  }

  /**
   * 启用产地
   * @param id
   */
  @Override
  public void enabled(Long id){
    //根据id获取产地并且判空
    Origin origin = this.getOriginById(id);

    origin.enabled();
    originRepository.update(origin);
  }

  /**
   * 停用产地
   * @param id
   */
  @Override
  public void disabled(Long id){
    //根据id获取产地并且判空
    Origin origin = this.getOriginById(id);

    origin.disabled();
    originRepository.update(origin);
  }

  /**
   * 根据id获取产地并且判空
   * @param id
   * @return
   */
  public Origin getOriginById(Long id){
    ExtAssert.notNull(id, "id为空");

    Origin origin = originRepository.getById(id);
    ExtAssert.notNull(origin, "对象为空");

    return origin;
  }
}
